package cn.tedu.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class TopicProducer {
    public static void main(String[] args) throws Exception{
        //1.配置连接信息
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5672);//5672是默认端口，可以省略
        cf.setUsername("guest");
        cf.setPassword("guest");
        //2.创建tcp连接对象
        Connection nc = cf.newConnection();
        //3.创建channel
        Channel cc = nc.createChannel();
        //4.定义direct类型的交换机
        cc.exchangeDeclare("topic_exchange", BuiltinExchangeType.TOPIC);
        //5.准备数据并发送
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.print("发送消息:");
            String msg = scanner.nextLine();
            if (msg.equals("exit"))
                break;
            System.out.print("路由键:");
            String routingKey = scanner.nextLine();
            cc.basicPublish("topic_exchange",routingKey,null,msg.getBytes());
        }
        nc.close();
    }
}
